//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "adminActions\fn_teleportToMe.sqf"
#include <core_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_params","_caller_name","_caller_uid","_player_uid","_player_name","_player_info","_action","_authorized","_player","_player_id","_counter","_player_pos","_new_pos","_all_players","_marker_name","_caller"];
PARAMS_3(_params,_caller_name,_caller_uid);

_authorized = _params select 0;
_action = _params select 1;
_player = _params select 2;
_player_uid = _params select 3;
_player_name = _params select 4;

if (isServer) then {
	_all_players = playableUnits;
	if (!isMultiPlayer) then { _all_players = [player]; };
	
	_caller = objNull;
	{ if (getPlayerUID _x == _caller_uid) exitWith { _caller = _x; };
	} foreach _all_players;
	
	_player_pos = [];
	if (!(isNull _caller)) then {
		_player_pos = getPosATL (vehicle _caller);
		_player_pos set [2, (_player_pos select 2) + 0.1];
	};
	
	_marker_name = switch (side _player) do {
		case west : { "respawn_west" };
		case east : { "respawn_east" };
		case resistance : { "respawn_guerrila" };
		default { "" };
	};
	
	
	_player = objNull;
	{ if (getPlayerUID _x == _player_uid) exitWith { _player = _x; };
	} foreach _all_players;

	if (!isNull _player && (count _player_pos > 0)) then {
		OSETVARP(_player,QGVAR(PLAYER_START_POS),_player_pos);
		DEBUG_2("Player Updated",_player_uid,_player_pos);
	} else {
		ERROR_1("Player Not Found",_player_uid);
	};
};
if (!isDedicated) then {
	if (_player_uid == getPlayerUID player) then {
		private ["_vec","_timeout"];
		_timeout = time + 10;
		waituntil { count(PGETVAR(QGVAR(PLAYER_START_POS),[])) > 0 || _timeout < time };
		_player_pos = PGETVAR(QGVAR(PLAYER_START_POS),(getPosATL player));
		if ((_player_pos distance player) > 30) then {
			titleText [format[localize "STR_MSO_AA_PLAYER_TELEPORTEDTOME",_caller_name], "BLACK",0.1];
			sleep 1;
			PSETVARP(QGVAR(PLAYER_START_POS),[]);
			while {vehicle player != player} do {
				_vec = vehicle player;
				player action ["eject", _vec];
				waituntil {vehicle player != _vec};
			};
			player setVelocity [0,0,0];
			player setPosATL _player_pos;
			titleFadeOut 5;
		};
	} else {

	};
};

TRACE("End");
TMFPROFILERSTOP;
